package com.gxwz.medical.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxwz.medical.domain.Area;
import com.gxwz.medical.domain.ChronicInfo;
import com.gxwz.medical.domain.Institution;
import com.gxwz.medical.domain.User;

/**
 * 农合经办机构管理控制处理模块
 * @author 演示
 *
 */
public class InstitutionServlet extends BaseServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String  m=request.getParameter("m");//动作类型参数
		if("list".equals(m)){
			process(request, response, "/page/institution/institution_list.jsp");
		}else if("input".equals(m)){
			process(request, response, "/page/institution/institution_add.jsp");
		}else if("get".equals(m)){
			process(request, response, "/page/institution/institution_edit.jsp");
		}else if("add".equals(m)){
			add(request, response);
		}else if("edit".equals(m)){
			edit(request, response);
		}else if("del".equals(m)){
			del(request, response);
		}
	}
	/**
	 * 添加机构方法
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		 //接收页面传递过来的参数
		 String areacode=request.getParameter("areacode");
		 String agencode=request.getParameter("agencode");
		 String agenname=request.getParameter("agenname");//有可能出现中文乱码
		 //实例化Institution类
		 Institution institution =new Institution();
		 int grade=areacode.length()==6?1:2;
		 //调用添加方法
		 try{
			 Area area =new Area(areacode);
			 institution.addInst(area, agencode, agenname, grade);
			 process(request, response, "/page/institution/institution_list.jsp");
		 }catch(Exception e){
			 error(request, response);
		 }
		 
	}

	private void edit(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{

	  	  //1：接收参数
		 String areacode=req.getParameter("areacode");
		 String agencode=req.getParameter("agencode");
		 String agenname=req.getParameter("agenname");//有可能出现中文乱码
		 //实例化Institution类
		 Institution institution=new Institution();
		 int grade=areacode.length()==6?1:2;
		 //调用添加方法
		 try{
			 Area area =new Area(areacode);
			 institution.edit(area,agencode,agenname,grade);
	  		process(req, resp,"/page/institution/institution_list.jsp");
	  	  }catch(Exception e){
	  		  error(req, resp);
	  	  }		  
		}
	
	private void del(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{

      try{
		  	  //1：接收参数
	  	  	String areacode=req.getParameter("id");
	  	  	  
	  	  	  //2:构造新慢病信息对象
	  	  	Institution model=new Institution();
	  		model.del(areacode);
	  		process(req, resp, "/page/institution/institution_list.jsp");
	  	  }catch(Exception e){
	  		e.printStackTrace();
	  	  }	
	}
}
	
